/**
* Author:LSP
* Date:2020-12-21
*/
// @导入引用开发环境
if (typeof require === 'undefined') require = importModule
const { Base } = require("./lsp环境")

// @小组件配置
const widgetConfigs = {
    // 直接指定鸡精id
    defaultFundId: '001838,002190,161725,160643,000336,161726,519019',
    // 是否是iPhone12mini
    isIphone12Mini: false,
    refreshInterval: 0, // 缓存刷新时间--估算(单位：分钟)
    // 预览模式：0：小尺寸，1：中等尺寸，2：大尺寸，负数：不预览
    previewMode: 1,

    selectPicBg: true, // 选择图片
    colorBgMode: false,  // 纯色背景模式
    bgColor: Color.black(), // 黑色背景

    // 鸡精名字体
    fundFont: new Font('Menlo', 11),
    // 鸡精净值字体
    fundExpectWorthFont: Font.boldSystemFont(12),
    // 鸡精日期字体
    fundDateFont: new Font('Menlo', 11),
    // 鸡精涨的字体颜色
    riseUpColor: new Color("EA4041", 0.8),
    // 鸡精跌的字体颜色
    fallDownColor: new Color("1BA784", 0.8),

    padding: {
        top: 10, // 上边距
        left: 10, // 左边距
        bottom: 10, // 底边距
        right: 2, // 右边距
    },
}

// @定义小组件
class Widget extends Base {
    constructor(scriptName) {
        super(scriptName)
        // 初始化其他变量
        this.setSelectPicBg(widgetConfigs.selectPicBg)
        this.setColorBgMode(widgetConfigs.colorBgMode, widgetConfigs.bgColor)
        this.paddingSetting(Object.assign(widgetConfigs.padding))
        this.refreshIntervalTime(widgetConfigs.refreshInterval)
        this.setPreViewSizeMode(widgetConfigs.previewMode)
    }

    //-------------------------------------

    /**
     * 基金数据获取
     * @param {Array} fidArr 鸡精id数组
     */
    async getFund(fidArr) {
        // 拼接基金id
        let fidFull = ''
        for (let fid of fidArr) {
            fidFull = fidFull + fid + ","
        }
        fidFull = fidFull.substring(0, fidFull.lastIndexOf(','))

        // 请求基金数据
        const url = `https://api.doctorxiong.club/v1/fund?code=${fidFull}`
        const response = await this.httpGet(url)

        return response.data
    }

    //-------------------------------------

    /**
     * @渲染小组件
     */
    async render() {
        //-------------------------------------
        // 鸡精id数组
        const fidArr = widgetConfigs.defaultFundId.replace(/\，/g, ",").split(",")
        // 进行请求数据
        const fundData = await this.getFund(fidArr)
        // 数据排序
        const sortedFunData = fundData.sort(function (a, b) { return b.expectGrowth - a.expectGrowth })
        console.log(`----------------------------------------`)
        console.log(``)

        //-------------------------------------
        const widget = new ListWidget()
        //-------------------------------------

        let index = 0
        for (const fund of sortedFunData) {
            index = index + 1
            //创建水平方向stack
            const hStack = widget.addStack()
            hStack.layoutHorizontally()
            // 直接跳转支付宝基金页面
            hStack.url = 'alipays://platformapi/startapp?appId=20000793'
            // 颜色值
            const expectGrowth = fund.expectGrowth // 当前基金单位净值估算日涨幅
            let fundColor = widgetConfigs.riseUpColor
            if (expectGrowth < 0) {
                fundColor = widgetConfigs.fallDownColor
            }

            // 基金名称
            const title = hStack.addText(`>› ${fund.name}`)
            title.font = widgetConfigs.fundFont
            title.textColor = fundColor
            title.lineLimit = 1

            // 净值估算更新日期 
            let expectWorthDateStr = fund.expectWorthDate
            let expectWorthDateText = fund.netWorthDate
            // 当前基金单位净值估算日涨幅,单位为百分比 
            let expectGrowthStr = expectGrowth
            if (expectWorthDateStr.search(expectWorthDateText) != -1) {
                expectGrowthStr = fund.dayGrowth
            }
            if (expectWorthDateStr != undefined && expectWorthDateStr.length > 5) {
                expectWorthDateText = expectWorthDateStr.substring(5)
            }

            // 净值
            hStack.addSpacer(6)
            const expectWorth = hStack.addText(`‹ ${expectGrowthStr} ›`)
            expectWorth.font = widgetConfigs.fundExpectWorthFont
            expectWorth.textColor = widgetConfigs.riseUpColor
            expectWorth.lineLimit = 1

            // 净值估算更新日期
            hStack.addSpacer(15)
            const expectWorthDate = hStack.addText(`${expectWorthDateText}`)
            expectWorthDate.font = widgetConfigs.fundDateFont
            expectWorthDate.textColor = fundColor
            expectWorthDate.lineLimit = 1
            expectWorthDate.textOpacity = 0.9

            // 左对齐
            hStack.addSpacer()
            if(index != sortedFunData.length) {
                widget.addSpacer(5)
            }

        }

        //-------------------------------------
        return widget
        //-------------------------------------
    }
}

// @运行测试
const { Running } = require("./lsp环境")
await Running(Widget, Script.name())